﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Data;
using System.Reflection;
using System.IO;
/*
 * Author:W
 * 数据库操作
 */
namespace W.GameFrameWork.ExcelTool
{
	public class SQLHelper
	{

		/// <summary>
		/// 读取数据库某张表文件
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="fileName"></param>
		/// <returns></returns>
		public static List<T> ReadSQLFile<T>(string fileName)
		{
			string dbFullName = "Config.db";
			string dbFilePath = PathUtil.GetSqlDir(dbFullName) + "/"+dbFullName;			
			SqliteDatabase sqliteDatabase = new SqliteDatabase(dbFilePath);
			string sqlStr = "select * from " + fileName;
			SqlDataTable readerData = sqliteDatabase.ExecuteQuery(sqlStr);

			List<T> objList = new List<T>();
			
			System.Type classType = typeof(T);
			Assembly assembly = Assembly.GetAssembly(classType);//获取泛型的程序集
			List<FieldInfo> fieldInfos = new List<FieldInfo>(classType.GetFields());//获取到泛型所有字段的集合
			List<string> fieldNames = readerData.Columns;

			for (int i = 0; i < readerData.Rows.Count; i++)
			{
				object obj = assembly.CreateInstance(classType.FullName); //泛型实例化
				string fieldValue;
				for (int j = 0; j < fieldNames.Count; j++)
				{
					FieldInfo fieldInfo = fieldInfos.Find(t => t.Name.Equals(fieldNames[j]));
					if (fieldInfo != null)
					{
						//Debug.Log("propertyInfo Name="+propertyInfo.Name+" Type="+propertyInfo.PropertyType);
						fieldValue = readerData.Rows[i][fieldNames[j]].ToString();

						if (fieldInfo.FieldType.Equals(typeof(System.Int32[])))
						{
							int[] intArr = GetIntArr(fieldValue);
							fieldInfo.SetValue((T)obj, intArr);
						}
						else if (fieldInfo.FieldType.Equals(typeof(System.String[])))
						{
							string[] strArr = fieldValue.Split(';');
							fieldInfo.SetValue((T)obj, strArr);
						}
						else if (fieldInfo.FieldType.Equals(typeof(System.Single[])))
						{
							float[] floatArr = GetFloatArr(fieldValue);
							fieldInfo.SetValue((T)obj, floatArr);

						}
						else if (fieldInfo.FieldType.Equals(typeof(int)))
						{
							fieldInfo.SetValue((T)obj, int.Parse(fieldValue));
						}
						else if (fieldInfo.FieldType.Equals(typeof(string)))
						{
							fieldInfo.SetValue((T)obj, fieldValue);
						}
						else if (fieldInfo.FieldType.Equals(typeof(float)))
						{
							fieldInfo.SetValue((T)obj, float.Parse(fieldValue));
						}

					}
				}

				objList.Add((T)obj);
			}

			return objList;
		}


		/// <summary>
		/// 将分割字符串转换成特定数据类型数组
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="type"></param>
		/// <returns></returns>
		private static int[] GetIntArr(string arrStr)
		{
			string[] arr = arrStr.Split(';');
			int[] resArr = new int[arr.Length];
			for (int i = 0; i < arr.Length; i++)
			{
				resArr[i] = int.Parse(arr[i]);
			}
			return resArr;
		}


		/// <summary>
		/// 将分割字符串转换成特定数据类型数组
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="type"></param>
		/// <returns></returns>
		private static float[] GetFloatArr(string arrStr)
		{
			string[] arr = arrStr.Split(';');
			float[] resArr = new float[arr.Length];
			for (int i = 0; i < arr.Length; i++)
			{
				resArr[i] = float.Parse(arr[i]);
			}
			return resArr;
		}
	}
}

